package edu.iscas.appcheckclient.model.actions;

import android.util.Log;

import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import java.io.Reader;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import edu.iscas.appcheckclient.util.GsonUtil;

public class Request {
    protected List<Action> actionList;
    protected List<Action> mockedHttpResponseList;
    protected List<Action> mockedLocationList;

    protected String appName;
    protected String name;
    protected String packageName;
    protected boolean reproduceTiming;
    protected String startActivity;
    protected String testDescription;
    protected Timestamp ts;

    static class RequestToken extends TypeToken<LinkedList<Request>>{}

    public Request(String appName, String name, String packageName, boolean reproduceTiming, String startActivity, String testDescription) {
        this(new LinkedList<Action>(), new LinkedList<Action>(), new LinkedList<Action>(), appName, name, packageName, reproduceTiming, startActivity, testDescription);
    }

    public Request(List<Action> actionList, List<Action> mockedHttpResponseList, List<Action> mockedLocationList, String appName, String name, String packageName, boolean reproduceTiming, String startActivity, String testDescription) {
        this.actionList = actionList;
        this.mockedHttpResponseList = mockedHttpResponseList;
        this.mockedLocationList = mockedLocationList;
        this.appName = appName;
        this.name = name;
        this.packageName = packageName;
        this.reproduceTiming = reproduceTiming;
        this.startActivity = startActivity;
        this.testDescription = testDescription;
    }

    public static Request fromJSON(Reader reader){
        return GsonUtil.buildGson().fromJson(reader, Request.class);
    }

    public static Request fromJSON(String str){
        Log.d("Request", str);
        return GsonUtil.buildGson().fromJson(str, Request.class);
    }

    public static List<Request> listFromJSON(String str){
        return (List)GsonUtil.buildGson().fromJson(str, new RequestToken().getType());
    }

    public static String listToJSON(List<Request> requestList){
        return GsonUtil.buildGson().toJson(requestList);
    }

    public void addAction(Action action){
        getActionList().add(action);
    }

    public void addActionList(List<Action> actionList){
        getActionList().addAll(actionList);
    }

    public String getAppName() {
        return appName;
    }

    public String getName() {
        return name;
    }

    public String getPackageName() {
        return packageName;
    }

    public boolean isReproduceTiming() {
        return reproduceTiming;
    }

    public String getStartActivity() {
        return startActivity;
    }

    public String getTestDescription() {
        return testDescription;
    }

    public Timestamp getTs() {
        return ts;
    }

    public List<Action> getActionList() {
        return actionList;
    }

    public List<Action> getMockedHttpResponseList() {
        return mockedHttpResponseList;
    }

    public List<Action> getMockedLocationList() {
        return mockedLocationList;
    }

    public String toJSON(){
        return GsonUtil.buildGson().toJson(this);
    }

    public JsonElement toJsonElement(){
        return GsonUtil.buildGson().toJsonTree(this);
    }
}
